package com.supostacks.application.server.es;


import com.alibaba.fastjson.JSONObject;
import com.supostacks.domain.vo.BaseVo;
import com.supostacks.domain.vo.ResourceInfoVo;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class EsResourceInfoRepository {

    @Resource
    public ElasticsearchRestTemplate elasticsearchRestTemplate;

    public void save(ResourceInfoVo resourceInfoVo) {
        elasticsearchRestTemplate.save(resourceInfoVo);
    }

    public ResourceInfoVo selectById(String id) {
        return elasticsearchRestTemplate.get(id, ResourceInfoVo.class);
    }

    public void createIdx(){
        elasticsearchRestTemplate.indexOps(ResourceInfoVo.class).create();
    }

    public boolean exists() {
        return elasticsearchRestTemplate.indexOps(ResourceInfoVo.class).exists();
    }

    public void deleteById(String id) {
        elasticsearchRestTemplate.delete(id, ResourceInfoVo.class);
    }

    public List<BaseVo> search(JSONObject option) {
        String keyword = option.getString("keyword");
        String opt = option.getString("opt");
        int page = option.getInteger("page") - 1;
        int pageSize = option.getInteger("pageSize");
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder().
                withPageable(PageRequest.of(page, pageSize)).
                withHighlightFields(new HighlightBuilder.Field(keyword));

        if (StringUtils.hasText(keyword)) {
            if ("category".equals(opt)) {
                queryBuilder = queryBuilder.withQuery(QueryBuilders.matchQuery("categoryName", keyword));
            } else {
                queryBuilder = queryBuilder.withQuery(QueryBuilders.matchQuery("subject", keyword)).
                        withQuery(QueryBuilders.matchQuery("content", keyword));
            }
        }

        NativeSearchQuery nativeSearchQuery = queryBuilder.build();

        SearchHits<ResourceInfoVo> search = elasticsearchRestTemplate.search(nativeSearchQuery, ResourceInfoVo.class);

        List<SearchHit<ResourceInfoVo>> searchHits = search.getSearchHits();
        List<BaseVo> result = new ArrayList<>();
        for (SearchHit<ResourceInfoVo> hit : searchHits) {
            ResourceInfoVo content = hit.getContent();
            result.add(content);
        }

        return result;
    }
}
